/*
 * Copyright (c) 2012 Andrea De Rinaldis <ozioso at ipignoli.dyndns.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dyndns.ipignoli.droidcruise.recording;

import java.util.Calendar;
import java.util.GregorianCalendar;
import org.dyndns.ipignoli.droidcruise.position.MyGeocoder;
import org.dyndns.ipignoli.droidcruise.position.MyLocation;
import org.dyndns.ipignoli.droidcruise.util.MyDateFormat;


public class Recording{
  public static final int DEFAULT_LIMIT = 130;

  public final static int TUTOR_TYPE    = 0;
  public final static int RADAR_TYPE    = 1;
  public final static int ROUTE_TYPE    = 2;

  protected static String getDefaultLocality(double latitude, double longitude){
    return new StringBuffer(String.valueOf(latitude)).append(",")
        .append(longitude).toString();
  }

  private long              _id;
  private int               type;
  private GregorianCalendar date;
  private String            locality;
  private boolean           localityOK;
  private double            latitude;
  private double            longitude;
  private int               limit;
  private String            comment;
  private boolean           sent;

  protected MyGeocoder      geocoder;

  public Recording(long _id, int type, MyLocation location, int limit,
      String comment, boolean sent){
    this(_id, type, location.getTime(), getDefaultLocality(
        location.getLatitude(), location.getLongitude()), false, location
        .getLatitude(), location.getLongitude(), limit, comment, sent);
  }

  public Recording(long _id, int type, long date, String locality,
      boolean localityOK, double latitude, double longitude, int limit,
      String comment, boolean sent){
    setId(_id);
    setType(type);
    setDate(date);
    setLatitude(latitude);
    setLongitude(longitude);
    setLimit(limit);
    setComment(comment);
    setSent(sent);
    geocoder = new MyGeocoder();
    setLocality(locality, localityOK);
  }

  public long getId(){
    return _id;
  }

  public void setId(long _id){
    this._id = _id;
  }

  public int getType(){
    return type;
  }

  public void setType(int type){
    this.type = type;
  }

  public long getTime(){
    return date.getTimeInMillis();
  }

  public GregorianCalendar getDate(){
    return date;
  }

  public void setDate(long date){
    setDate(MyDateFormat.getInstance().createGC(date));
  }

  public void setDate(GregorianCalendar date){
    date.set(Calendar.MILLISECOND, 0);
    this.date = date;
  }

  public String getLocality(){
    return locality;
  }

  public void setLocality(String locality){
    this.locality = locality;
  }

  public void setLocality(String locality, boolean localityOK){
    if(localityOK){
      setLocality(locality);
      setLocalityOK(localityOK);
      return;
    }

    try{
      String l = geocoder.getFromLocation(getLatitude(), getLongitude());
      if(l == null)
        throw new Exception();
      setLocality(l);
      setLocalityOK(true);
    }catch(Exception e){
      setLocality(locality);
      setLocalityOK(false);
    }
  }

  public boolean getLocalityOK(){
    return localityOK;
  }

  public void setLocalityOK(boolean localityOK){
    this.localityOK = localityOK;
  }

  public double getLatitude(){
    return latitude;
  }

  public void setLatitude(double latitude){
    this.latitude = latitude;
  }

  public double getLongitude(){
    return longitude;
  }

  public void setLongitude(double longitude){
    this.longitude = longitude;
  }

  public int getLimit(){
    return limit;
  }

  public void setLimit(int limit){
    this.limit = limit;
  }

  public String getComment(){
    return comment;
  }

  public void setComment(String comment){
    this.comment = comment;
  }

  public boolean getSent(){
    return sent;
  }

  public void setSent(boolean sent){
    this.sent = sent;
  }
}
